package com.hmkj.core.service.wx.impl;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.common.baseMapper.GenericPo;
import com.hmkj.core.mapper.wx.WxButtonMapper;
import com.hmkj.core.po.wx.WxButton;
import com.hmkj.core.service.wx.WxButtonService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 微信公众号自定义菜单按钮表业务相关Service接口实现<br>
 *
 * @ClassName: WxButtonServiceImpl
 * @author Vector
 * @date   2017-10-18 10:04:13
 */
@Service
@Scope("prototype")
@Transactional(rollbackFor = Exception.class)
public class WxButtonServiceImpl implements WxButtonService {

	@Resource
    private WxButtonMapper wxButtonMapper;

    @Override
    public GenericMapper<WxButton,Integer> _getMapper() {
        return wxButtonMapper;
    }

    @Override
    public void deleteByWxId(Integer wxId) {
        WxButton model = new WxButton();
        model.setWxId(wxId);
        wxButtonMapper.delete(model);
    }

    @Override
    public List<WxButton> findSubMenuById(Integer bid) {
        WxButton model = new WxButton();
        model.setTopBtn(bid);
        model.setDelFlag(GenericPo.DELFLAG.NORMAL.code);
        return wxButtonMapper.select(model);
    }

    @Override
    public void updateAll(WxButton[] subBtns) {
        // 先删除所有菜单
        WxButton model = new WxButton();
        model.setTopBtn(subBtns[0].getTopBtn());
        wxButtonMapper.delete(model);
        if (subBtns==null||subBtns.length<=0){
            return;
        }
        for (int i=0;i<subBtns.length;i++){
            WxButton btn = subBtns[i];
            btn.setId(null);
            wxButtonMapper.insert(btn);
        }

    }
}
